﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VoltGameEditor
{
	public partial class ObjectPane : WeifenLuo.WinFormsUI.Docking.DockContent
	{
		public ObjectPane()
		{
			InitializeComponent();
		}

		private void ObjectPane_Load(object sender, EventArgs e)
		{
			
		}

		private void treeEnts_ItemDrag(object sender, ItemDragEventArgs e)
		{
			TreeNodeEx n = e.Item as TreeNodeEx;
			TreeNodeEx node = new TreeNodeEx();
			if (e.Button == MouseButtons.Left)
			{
				if (n.Level == 2 || n.ItemType == MapObject.Type.CustomObject)
				{
					node.Text = n.Text;
					node.DefaultKVPs = n.DefaultKVPs;
					node.ItemType = n.ItemType;
					node.ObjIndex = n.ObjIndex;
					node.ToolTipText = n.ToolTipText;
					node.Tag = n.Tag;
					treeEnts.DoDragDrop(node, DragDropEffects.Copy | DragDropEffects.Scroll);
				}
			}
		}

		private void tabControl2_Deselecting(object sender, TabControlCancelEventArgs e)
		{
			int i = mapDef.GetDocumentIndex();

			if (mapDef.MapData[i].Tree.SelectedNode == null)
				return;
			TreeNodeEx node = (TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode;
			int j = node.ObjIndex;

			if (tabControl2.SelectedIndex == 0)
			{
				if (mapDef.MapData[i].Tree.SelectedNode != null)
				{
					switch (((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ItemType)
					{
						case MapObject.Type.Brush:
							{
								MapObject.SetKVP2DataGrid(mapDef.MapData[i].Brushes[j].KVPs, node);
								break;
							}
						case MapObject.Type.Entity:
							{
								MapObject.SetKVP2DataGrid(mapDef.MapData[i].Entities[j].KVPs, node);
								break;
							}
						case MapObject.Type.Group:
							{
								MapObject.SetKVP2DataGrid(mapDef.MapData[i].Groups[j].KVPs, node);
								break;
							}
						case MapObject.Type.TwoDeeObj:
							{
								MapObject.SetKVP2DataGrid(mapDef.MapData[i].TwoDeeObjects[j].KVPs, node);
								break;
							}
						case MapObject.Type.Worldspawn:
							{
								MapObject.SetKVP2DataGrid(mapDef.MapData[i].Worldspawn.KVPs, node);
								break;
							}
					}
				}
			}
			else if (tabControl2.SelectedIndex == 1)
			{
				DictPropGridAdapter ga = (DictPropGridAdapter)pGrid_obj.SelectedObject;
				switch (((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ItemType)
				{
					case MapObject.Type.Brush:
						{
							mapDef.MapData[i].Brushes[((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ObjIndex].KVPs = ga.Dictionary;
							break;
						}
					case MapObject.Type.Entity:
						{
							mapDef.MapData[i].Entities[((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ObjIndex].KVPs = ga.Dictionary;
							break;
						}
					case MapObject.Type.Group:
						{
							mapDef.MapData[i].Groups[((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ObjIndex].KVPs = ga.Dictionary;
							break;
						}
					case MapObject.Type.TwoDeeObj:
						{
							mapDef.MapData[i].TwoDeeObjects[((TreeNodeEx)mapDef.MapData[i].Tree.SelectedNode).ObjIndex].KVPs = ga.Dictionary;
							break;
						}
					case MapObject.Type.Worldspawn:
						{
							mapDef.MapData[i].Worldspawn.KVPs = ga.Dictionary;
							break;
						}
				}
				
			}
				Globals.tabIndex_Sel = tabControl2.SelectedIndex;
		}

		private void treeEnts_KeyDown(object sender, KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Delete:
					TreeNodeEx node = (TreeNodeEx)treeEnts.SelectedNode;;
					if (node.ItemType == MapObject.Type.CustomObject) //UNHANDLED EXCEPTION!
						treeEnts.SelectedNode.Remove();
					break;
			}
		}
	}
}
